import { defineStore } from 'pinia'
import { ref } from 'vue'
import { course } from '@/api/course'
import { useRouter } from 'vue-router'

export const UseCourseStore = defineStore(
  'use-course-store',
  () => {
    //定义数据
    const router = useRouter()
    const totalData = ref([])
    const detailData = ref([])

    //拿到用户的学号并且传递给后端,并且进行判断用户点击的是哪一个
    const Id = ref('')
    const userInfo = ref('')
    userInfo.value = JSON.parse(localStorage.getItem('info'))
    Id.value = userInfo.value.stuId
    var ids = Id.value
    console.log(ids)
    const ListData = async () => {
      const res = await course(Id)
      totalData.value = res.data
      console.log(totalData.value)
    }
    //定义方法
    const CourseData = (id) => {
      console.log(id)
      totalData.value.forEach((item) => {
        if (id == item.courseId) {
          //   console.log(item)
          detailData.value = item
          console.log(detailData)
          router.push(`/detail/${id}`)
        }
      })
    }

    ListData()
    return {
      totalData,
      ListData,
      CourseData,
      detailData
    }
  },
  {
    persist: true
  }
)
